package com.coursehub.exercise.service.impl;

import com.coursehub.exercise.entity.FavoriteQuestionBank;
import com.coursehub.exercise.entity.FavoriteQuestionBankQuestion;
import com.coursehub.exercise.entity.FavoriteQuestionBankQuestionId;
import com.coursehub.exercise.repository.FavoriteQuestionBankQuestionRepository;
import com.coursehub.exercise.repository.FavoriteQuestionBankRepository;
import com.coursehub.exercise.service.FavoriteQuestionBankService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
public class FavoriteQuestionBankServiceImpl implements FavoriteQuestionBankService {

    private final FavoriteQuestionBankRepository favoriteQuestionBankRepository;
    private final FavoriteQuestionBankQuestionRepository favoriteQuestionBankQuestionRepository;

    @Override
    public FavoriteQuestionBank create(String favoriteQuestionBankId, String userId, String courseId) {
        FavoriteQuestionBank bank = new FavoriteQuestionBank();
        bank.setFavoriteQuestionBankId(favoriteQuestionBankId);
        bank.setUserId(userId);
        bank.setCourseId(courseId);
        return favoriteQuestionBankRepository.save(bank);
    }

    @Override
    public void delete(String favoriteQuestionBankId) {
        favoriteQuestionBankRepository.deleteById(favoriteQuestionBankId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<FavoriteQuestionBank> listByUser(String userId) {
        return favoriteQuestionBankRepository.findByUserId(userId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<FavoriteQuestionBank> listByUserAndCourse(String userId, String courseId) {
        return favoriteQuestionBankRepository.findByUserIdAndCourseId(userId, courseId);
    }

    @Override
    public void addQuestion(String favoriteQuestionBankId, String questionId) {
        if (favoriteQuestionBankQuestionRepository.existsByIdFavoriteQuestionBankIdAndIdQuestionId(favoriteQuestionBankId, questionId)) {
            return;
        }
        FavoriteQuestionBankQuestion entity = new FavoriteQuestionBankQuestion();
        entity.setId(new FavoriteQuestionBankQuestionId(favoriteQuestionBankId, questionId));
        favoriteQuestionBankQuestionRepository.save(entity);
    }

    @Override
    public void removeQuestion(String favoriteQuestionBankId, String questionId) {
        favoriteQuestionBankQuestionRepository.deleteByIdFavoriteQuestionBankIdAndIdQuestionId(favoriteQuestionBankId, questionId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<String> listQuestionIds(String favoriteQuestionBankId) {
        return favoriteQuestionBankQuestionRepository.findByIdFavoriteQuestionBankId(favoriteQuestionBankId)
                .stream().map(e -> e.getId().getQuestionId()).collect(Collectors.toList());
    }
}


